﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace Excel
{
    public partial class FExcel : Form
    {
        public FExcel()
        {
            InitializeComponent();
            btnRead.Enabled = false;
            btnClear.Enabled = false;
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Filter = "Excel Files|*.xlsx;*.xls";
            if (openDialog.ShowDialog() != DialogResult.OK) return;
            if (openDialog.FileName.Length <= 0) return;
            txtPath.Text = openDialog.FileName;
            if(!String.IsNullOrEmpty(txtPath.Text))
                btnRead.Enabled = true;
            else
                btnRead.Enabled = false; ;
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            try
            {
                dataGridView1.DataSource = Excel.Lib.ExcelHelper.Read(txtPath.Text).Tables[0];
                this.Cursor = Cursors.Default;
                btnClear.Enabled = true;
                
            }
            catch (Exception ex)
            {
                this.Cursor = Cursors.Default;
                MessageBox.Show(ex.Message);
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = null;
        }

        // Excel.Lib.ExcelHelper.Write(Excel.Lib.ExcelHelper.Read(textBox1.Text), "C://");
    }
}
